﻿using System;
using System.Collections.Generic;
using System.Management.Automation;
using ITSP.SharePoint.CacheRefresh.Examples.Configuration;
using ITSP.SharePoint.CacheRefresh.Interfaces;
using ITSP.SharePoint.CacheRefresh.Repositories;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell;

namespace ITSP.SharePoint.CacheRefresh.Examples.PowerShell
{
    /// <summary>
    /// Updates the Cache Timer Jobs 
    /// </summary>
    [Cmdlet("Set", "ITSPCacheExampleConfiguration", SupportsShouldProcess = true)]
    public class SetCacheExampleConfiguration : SPSetCmdletBase<ExampleConfiguration>
    {
        #region Cmdlet Parameters
        [Parameter(Mandatory = true, HelpMessage = "The Url of the SharePoint Web Application that is hosting the Cache Configuration List")]
        public string Url;

         [Parameter(Mandatory = true, HelpMessage = "The Configuration Object")]
        public ExampleConfiguration Configuration;

        #endregion

        protected override bool RequireUserFarmAdmin()
        {
            return false;
        }

        protected override bool RequireLocalFarmExist()
        {
            return true;
        }


        protected override void UpdateDataObject()
        {
            #region validation stuff

            // ensure can hit farm
            SPFarm farm = SPFarm.Local;
            if (farm == null)
            {
                ThrowTerminatingError(new InvalidOperationException("SharePoint farm not found."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
            }

            #endregion

            string cacheHostUrl = Url;
           
            Uri cacheHostUri = new Uri(cacheHostUrl);

            ExampleConfigurationManager manager = new ExampleConfigurationManager(cacheHostUri.ToString());
            manager.Configuration = Configuration;
            
            WriteObject(Configuration);
            }

        }
    
    /// <summary>
    /// Gets the Example Configuration
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "ITSPCacheExampleConfiguration", SupportsShouldProcess = true)]
    public class GetCacheExampleConfiguration : SPGetCmdletBase<ExampleConfiguration>
    {
        #region Cmdlet Parameters

        [Parameter(Mandatory = true, HelpMessage = "The Web Application Url")]
        [ValidateNotNullOrEmpty]
        public string Url;

        #endregion

        protected override bool RequireUserFarmAdmin()
        {
            return false;
        }

        protected override bool RequireLocalFarmExist()
        {
            return true;
        }
        
        protected override IEnumerable<ExampleConfiguration> RetrieveDataObjects()
        {
            #region validation stuff

            // ensure can hit farm
            SPFarm farm = SPFarm.Local;
            if (farm == null)
            {
                ThrowTerminatingError(new InvalidOperationException("SharePoint farm not found."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
            }

            #endregion

             ExampleConfigurationManager manager = new ExampleConfigurationManager(Url);
            return new List<ExampleConfiguration>{manager.Configuration};
        }

    }
    
}
